package com.lzw.mall.admin.controller;

import com.lzw.mall.common.entity.Member;
import com.lzw.mall.common.service.IMemberService;
import com.lzw.mall.common.util.Constants;
import com.lzw.mall.common.vo.LayuiTableVO;
import com.lzw.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;

    @GetMapping("/index")
    public String page(){
        return "member";
    }

    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVO list(@RequestParam Integer page, @RequestParam Integer limit, String keyword, String sex,String status){
        return memberService.list(page, limit, keyword,sex,status);
    }

    @ResponseBody
    @PostMapping("/save")
    public ResultVO save(Member m) {
        return memberService.doSave(m);
    }

    @ResponseBody
    @GetMapping("/selectById")
    public ResultVO selectById(Integer id){
        return memberService.doSelectById(id);
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public ResultVO updatePassword(String oldPassword, String newPassword, String againPassword){
        System.out.println(oldPassword);
        return memberService.updatePassword(oldPassword,newPassword,againPassword);
    }
    @PostMapping("/updateMima")
    @ResponseBody
    public ResultVO updateMima(Integer id){
        return memberService.updateMima(id);
    }

    @PostMapping("/openMember")
    @ResponseBody
    public void openMember(@RequestParam("ids[]") Integer[] ids ){
        memberService.openMember(ids);
    }
    @PostMapping("/closeMember")
    @ResponseBody
    public void closeMember(@RequestParam("ids[]") Integer[] ids ){
        memberService.closeMember(ids);
    }


    @ResponseBody
    @PostMapping("/avatar")
    public com.lzw.mall.admin.vo.ResultVO avatar(MultipartFile file){
        com.lzw.mall.admin.vo.ResultVO vo=new com.lzw.mall.admin.vo.ResultVO();
        String fileName=System.currentTimeMillis() + ".png";
        File f=new File("D:\\test01\\member\\"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Member m=new Member();
        m.setAvatar(Constants.UPLOAD_PATH+"member\\"+fileName);
        vo.setCode(0);
        vo.setData(m);
        return vo;
    }
}
